//
//  OMTImagePickerBuilder.m
//  OMTImagePicker
//
//  Created by yizhaorong on 2017/12/10.
//

#import "OMTImagePickerBuilder.h"
#import "UIImage+OMTBundle.h"
#import "NSBundle+OMTImagePicker.h"
#import "OMTAlbumManager.h"

@implementation OMTImagePickerBuilder

+ (instancetype)defaultBuilder {
    OMTImagePickerBuilder *config = [OMTImagePickerBuilder new];
    // 状态栏风格
    config.statusBarStyle = UIStatusBarStyleLightContent;
    // 正常色值
    config.normalColor = [UIColor whiteColor];
    // 不可点击色值
    config.disableColor = [UIColor colorWithWhite:1 alpha:0.5];
    // 允许不选择图片
    config.allowNotChoose = YES;
    // 默认修正方向
    config.shouldFixOrientation = YES;
    // 默认不显示视频
    config.showVedio = NO;
    // 默认显示图片
    config.showImage = YES;
    // 默认不选择原图
    config.shouldSelectOriginImage = NO;
    // 默认最大选择1张
    config.maxSelectCount = 1;
    // 默认不显示完成按钮
    config.showCompleteButton = NO;
    // 导出图片的宽度，默认828像素宽
    config.photoWidth = 828;
    // 默认3列
    config.columnCount = 3;
    // 最小可选中的图片宽度，默认是0，小于这个宽度的图片不可选中
    config.minPhotoWidthSelectable = 0;
    config.minPhotoHeightSelectable = 0;
    // 默认最大时长为10分钟
    config.maxVideoDuration = 10 * 60;
    // 默认显示不可选图片
    config.hideWhenCanNotSelect = NO;
    // 默认不显示录制视频
    config.shouldRecordVideo = NO;
    // 默认显示拍照
    config.shouldTakePhoto = YES;
    config.imageWithNamed = ^UIImage *(NSString *imageNamed) {
        if ([imageNamed isEqualToString:@"omt_nav_bg"]) {
            CGFloat navigationBarH = ([[UIApplication sharedApplication] statusBarFrame].size.height) + 44;
            CGFloat navigationBarW = [UIScreen mainScreen].bounds.size.width;
            UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, navigationBarW, navigationBarH)];
            [OMTImagePickerBuilder setGradientLayer:bgView];
            return [OMTImagePickerBuilder screenShot:bgView];
        }
        return [UIImage omt_bundleImageNamed:imageNamed];
    };
    config.localizedString = ^NSString *(NSString *key) {
        return [NSBundle omt_localizedStringForKey:key];
    };
    return config;
}

#pragma mark - 设置渐变色
+ (void)setGradientLayer:(UIView *)view {
    [self setGradientLayer:view frame:view.bounds];
}
+ (void)setGradientLayer:(UIView *)view frame:(CGRect)frame {
    CAGradientLayer *gradientLayer = [self getGradientLayerDefaultColorWithFrame:frame];
    [self _insertGradientLayer:gradientLayer toView:view];
}

+ (void)setGradientLayer:(UIView *)view colors:(NSArray <UIColor *> *)colors {
    CAGradientLayer *gradientLayer = [self getGradientLayerWithFrame:view.bounds colors:colors];
    [self _insertGradientLayer:gradientLayer toView:view];
}


/**
 在view上插入渐变色layer
 
 @param gradientLayer 渐变layer
 @param view 渐变色的view
 */
+ (void)_insertGradientLayer:(CAGradientLayer *)gradientLayer toView:(UIView *)view{
    NSMutableArray *gradientLayers = [NSMutableArray array];
    for (CALayer *sublayer in view.layer.sublayers) {
        if ([sublayer isKindOfClass:[CAGradientLayer class]]) {
            [gradientLayers addObject:sublayer];
        }
    }
    
    for (CAGradientLayer *gradientLayer in gradientLayers) {
        [gradientLayer removeFromSuperlayer];
    }
    
    [view.layer insertSublayer:gradientLayer atIndex:0];
}

+ (CAGradientLayer *)getGradientLayerDefaultColorWithFrame:(CGRect)frame {
    
    NSArray *colors = @[(id)OMTAlbumRGB(0x42c9cb), (id)OMTAlbumRGB(0x2fe0b4)];
    CAGradientLayer *gradientLayer = [self getGradientLayerWithFrame:frame colors:colors];
    return gradientLayer;
}

+ (CAGradientLayer *)getGradientLayerWithFrame:(CGRect)frame colors:(NSArray <UIColor *> *)colors {
    NSMutableArray *gradientColors = [NSMutableArray array];
    for (NSInteger i=0; i<colors.count; i++) {
        UIColor *color = colors[i];
        [gradientColors addObject:(__bridge id)color.CGColor];
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = gradientColors;
    gradientLayer.startPoint = CGPointMake(0, 1);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.frame = frame;
    return gradientLayer;
}

+ (UIImage *)screenShot:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    CALayer *rootLayer = view.layer;
    CGContextRef context = UIGraphicsGetCurrentContext();
    // doesn't tint tabBar-background in iOS7
    [rootLayer renderInContext:context];
    UIImage *rootLayerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return rootLayerImage;
}

@end
